<?php
ob_start();
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

$query="select ID from TrailHead where name like '$_POST[TrailHeadName]';";
$resultID=mysql_query($query);
if (!$resultID) {
  echo "Error selecting ID from TrailHead: " . mysql_error();
  die("");
}
$headID=mysql_result($result,0, "ID");

$query="
DELETE FROM TrailHead WHERE name like '$_POST[name]';
";
if (!mysql_query($query)) {
  echo "Error deleting entry from TrailHead: " . mysql_error();
}

$query="
INSERT INTO TrailHead (ID, name, gps, start_altitude, dog, bike)
VALUES (
	$headID,
	'$_POST[name]', 
	$_POST[gps],
	$_POST[start_altitude],
	'$_POST[dog]',
	'$_POST[bike]'
	)";

if (!mysql_query($query)) {
  echo "Error inserting entry into TrailHead: " . mysql_error();
}

mysql_close();
header("Location: index.php");
ob_flush();
?> 
